package com.github.llyb120.compiler.java;

/**
 * @Author: Administrator
 * @Date: 7/26/2021 11:16 PM
 */
public class DynamicJavaLoader extends ClassLoader{

    private CompileResult compileResult;

    public DynamicJavaLoader(CompileResult compileResult){
        this.compileResult = compileResult;
    }

    @Override
    public Class<?> loadClass(String name) throws ClassNotFoundException {
        Class<?> clz = null;
        if ((clz = findLoadedClass(name)) != null) {
            return clz;
        }
        if (name.equals(compileResult.getName())) {
            return clz = defineClass(name, compileResult.getByteCode(), 0, compileResult.getByteCode().length);
        }
        return DynamicJavaLoader.class.getClassLoader().loadClass(name);
    }
}
